package org.dromara.web.appInterface;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.dromara.app.appParams.*;
import org.dromara.app.appReturn.*;
import org.dromara.app.config.AppGoEasyUtil;
import org.dromara.app.config.AppUserActivityConfig;
import org.dromara.app.cssUtils.CssUtils;
import org.dromara.app.domain.SkYueJu;
import org.dromara.app.service.*;
import org.dromara.common.core.domain.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "约局相关接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/yueju")
@SaIgnore
public class AppYuanJuInterface {

    private final ISkYueJuUserService iSkYueJuUserService;

    private final ISkYueJuService iSkYueJuService;

    private final ISkYueJuPinglunService iSkYueJuPinglunService;

    private final AppGoEasyUtil appGoEasyUtil;

    private final IBfAppUserActivityService iBfAppUserActivityService;



    /**
     * 加入约局
     */
    @PostMapping("/joinYueJu")
    @Operation(summary = "加入约局", method = "post")
    public R joinYueJu(@RequestBody AppJoinYueJuParams params, HttpServletRequest req) {
        iSkYueJuUserService.joinYueJu(params, req);
        //添加活跃度
        iBfAppUserActivityService.addActivity(CssUtils.getUserId(req), AppUserActivityConfig.ZAI_XIAN_USER);
        return R.ok("已申请");
    }

    /**
     * 同意拒绝/约局
     */
    @PostMapping("/activeYueJu")
    @Operation(summary = "同意拒绝/约局", method = "post")
    public R activeYueJu(@RequestBody AppActiveYueJuParams params, HttpServletRequest req) throws Exception {
        iSkYueJuUserService.activeYueJu(params, req);
        return R.ok("操作成功");
    }

    /**
     * 用户进行取消
     */
    @PostMapping("/joinCencelYueJu")
    @Operation(summary = "用户进行取消", method = "post")
    public R joinCencelYueJu(@RequestBody AppJoinYueJuParams params, HttpServletRequest req) throws Exception {
        iSkYueJuUserService.joinCencelYueJu(params, req);
        return R.ok("取消成功");
    }

    /**
     * 已加入约局，用户进行退出
     */
    @PostMapping("/joinExitYueJu")
    @Operation(summary = "已加入约局，用户进行退出", method = "post")
    public R joinExitYueJu(@RequestBody AppJoinYueJuParams params, HttpServletRequest req) throws Exception {
        iSkYueJuUserService.joinExitYueJu(params, req);
        return R.ok("退出成功");
    }

    /**
     * 正在进行的约局列表(正在组局)
     *
     * @param req
     * @return
     */
    @PostMapping("/getUserYueJuList")
    @Operation(summary = "正在进行的约局列表(正在组局)", method = "post")
    public R<List<AppZhengZaiZuJuReturn>> getUserYueJuList(HttpServletRequest req) {
        List<AppZhengZaiZuJuReturn> list = iSkYueJuUserService.getUserYueJuList(req);
        return R.ok(list);
    }

    /**
     * 约局群组列表
     */
    @PostMapping("/getGroupList")
    @Operation(summary = "约局群组列表", method = "post")
    public R<List<AppZhengZaiZuJuReturn>> getGroupList(HttpServletRequest req) {
        List<AppZhengZaiZuJuReturn> list = iSkYueJuUserService.getGroupList(req);
        return R.ok(list);
    }

    /**
     * 我发起的约局
     */
    @PostMapping("/getMyFaQiGroupList")
    @Operation(summary = "我发起的约局", method = "post")
    public R<List<AppZhengZaiZuJuReturn>> getMyFaQiGroupList(@RequestBody AppPageParams params, HttpServletRequest req) {
        List<AppZhengZaiZuJuReturn> list = iSkYueJuUserService.getMyFaQiGroupList(params,req);
        return R.ok(list);
    }

    /**
     * 我参与的约局
     */
    @PostMapping("/getMyCanYuGroupList")
    @Operation(summary = "我参与的约局", method = "post")
    public R<List<AppZhengZaiZuJuReturn>> getMyCanYuGroupList(@RequestBody AppPageParams params, HttpServletRequest req) {
        List<AppZhengZaiZuJuReturn> list = iSkYueJuUserService.getMyCanYuGroupList(params,req);
        return R.ok(list);
    }

    /**
     * 约局详情
     */
    @PostMapping("/getYueJuInfo")
    @Operation(summary = "约局详情", method = "post")
    public R<AppYueJuListReturn> getYueJuInfo(@RequestBody AppDetailByIdParams params, HttpServletRequest req) {
        AppYueJuListReturn info = iSkYueJuService.getYueJuInfo(params,req);
        return R.ok(info);
    }

    /**
     * 评论约局
     */
    @PostMapping("/commentYueJu")
    @Operation(summary = "评论约局", method = "post")
    public R commentYueJu(@RequestBody AppPlingLunParams params, HttpServletRequest req) {
        iSkYueJuPinglunService.commentYueJu(params,req);
        return R.ok("评论成功");
    }

    /**
     * 约局评论列表
     */
    @PostMapping("/getCommentYueJuList")
    @Operation(summary = "约局评论列表", method = "post")
    public R getCommentYueJuList(@RequestBody AppPingLunpageParams params, HttpServletRequest req) {
        List<AppYueJuPingLunReturn> list = iSkYueJuPinglunService.getCommentYueJuList(params,req);
        return R.ok(list);
    }

    /**
     * 约局点赞
     */
    @PostMapping("/yueJuLikes")
    @Operation(summary = "约局点赞", method = "post")
    public R yueJuLikes(@RequestBody AppDetailByIdParams params, HttpServletRequest req) {
        iSkYueJuService.yueJuLikes(params,req);
        return R.ok("操作成功");
    }


    /**
     * 个人主页-项目活动列表
     */
    @PostMapping("/activeList")
    @Operation(summary = "个人主页-项目活动列表", method = "post")
    public R<List<AppActiveInfoReturn>> activeList(@RequestBody AppPageParams params, HttpServletRequest req) {
        List<AppActiveInfoReturn> list = iSkYueJuService.activeList(params,req);
        return R.ok(list);
    }

    /**
     * 申请加入群组列表（审核中）
     */
    @PostMapping("/getYueJuShengHeList")
    @Operation(summary = "申请加入群组列表（审核中）", method = "post")
    public R<List<AppYueJuListReturn>> getYueJuShengHeList(HttpServletRequest req) {
        List<AppYueJuListReturn> list = iSkYueJuService.getYueJuShengHeList(req);
        return R.ok(list);
    }

    /**
     * 审核约局人(同意)
     * @param req
     * @return
     */
    @PostMapping("/getShengHeTy")
    @Operation(summary = "审核约局人(同意)", method = "post")
    public R getShengHeTy(@RequestBody AppDetailByIdParams params, HttpServletRequest req) throws Exception {
         iSkYueJuUserService.getShengHeTy(params,req);
        return R.ok("审核成功");
    }
    /**
     * 审核约局人(拒绝)
     * @param req
     * @return
     */
    @PostMapping("/getShengHeJj")
    @Operation(summary = "审核约局人(拒绝)", method = "post")
    public R getShengHeJj(@RequestBody AppDetailByIdParams params, HttpServletRequest req) {
        iSkYueJuUserService.getShengHeJj(params,req);
        return R.ok("拒绝成功");
    }

    /**
     * 解散群聊
     */
    @PostMapping("/dissolveGroupChat")
    @Operation(summary = "解散群聊", method = "post")
    public R dissolveGroupChat(@RequestBody AppJieSanParams params, HttpServletRequest req) throws Exception {
        iSkYueJuUserService.dissolveGroupChat(params,req);
        return R.ok("请求成功");
    }


    /**
     * 我的群聊
     */
    @PostMapping("/getMyGroupChat")
    @Operation(summary = "我的群聊", method = "post")
    public R<List<AppYueJuListReturn>> getMyGroupChat(HttpServletRequest req) {
        Long userId = CssUtils.getUserId(req);
        List<AppYueJuListReturn> list = iSkYueJuUserService.getMyGroupChat(userId);
        return R.ok(list);
    }

    /**
     * 群组成员列表
     */
    @PostMapping("/getGroupUserList")
    @Operation(summary = "群组成员列表", method = "post")
    public R<List<AppUserInfo>> getGroupUserList(@RequestBody AppJoinYueJuParams app, HttpServletRequest req) {
        Long userId = CssUtils.getUserId(req);
        List<AppUserInfo> list = iSkYueJuUserService.getGroupUserList(app,userId);
        return R.ok(list);
    }


    /**
     * 编辑群公告、简介
     */
    @PostMapping("/updateGroupNoticeSynopsis")
    @Operation(summary = "编辑群公告、简介", method = "post")
    public R updateGroupNoticeSynopsis(@RequestBody AppYueJuGroupNodeParams app, HttpServletRequest req) {
        Long userId = CssUtils.getUserId(req);
        iSkYueJuService.updateGroupNoticeSynopsis(app,userId);
        return R.ok("请求成功");
    }

    /**
     * 群成员-踢人
     */
    @PostMapping("/groupKickingPeople")
    @Operation(summary = "群成员-踢人", method = "post")
    public R groupKickingPeople(@RequestBody AppYueJuGroupKickingPeople app, HttpServletRequest req) throws Exception {
        Long userId = CssUtils.getUserId(req);
        iSkYueJuUserService.groupKickingPeople(app,userId);
        return R.ok("请求成功");
    }

    /**
     * 群成员列表
     */
    @PostMapping("/queryGroupUserList")
    @Operation(summary = "群成员列表", method = "post")
    public void queryGroupUserList(@RequestBody AppJoinYueJuParams app, HttpServletRequest req) {
        iSkYueJuUserService.queryGroupUserList(app);
    }
}
